<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <a-spin :spinning="pageLoading">
    <template>
      <div class="table-page-search-wrapper">
        <a-card>
          <titleName title="基本信息" />
          <a-form-model
                  ref="ruleForm"
                  :model="fromObj"
          >
            <a-row :gutter="24">
              <a-col
                      :span="16"
              >
                <a-form-model-item
                        label="所属单位："
                        prop="projectId"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 18 }, sm: { span: 17 } }"
                >
                  <org-tree-select
                          is-need-auth
                          is-auto-select-first
                          check-strictly
                          disabled
                          v-model="fromObj.projectId"
                          @getProjectInfo="getProjectInfo"
                  />
                </a-form-model-item>
              </a-col>
              <a-col
                      :span="8"
              >
                <a-form-model-item
                        label="名称："
                        prop="name"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="fromObj.name" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col
                      :span="8"
              >
                <a-form-model-item
                        label="项目业态："
                        prop="name"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="projectInfo.projectFormatName" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col
                      :span="8"
              >
                <a-form-model-item
                        label="项目设计管理类别："
                        prop="name"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="projectInfo.designName" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col
                      :span="8"
              >
                <a-form-model-item
                        label="合同额(万)（不含内部分包）："
                        prop="name"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="ownerValue" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col
                      :span="8"
              >
                <a-form-model-item
                        label="总策划效益(万元)"
                        prop="name"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="fromObj.rate" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
              <a-form-model-item
                      label="策划效益率"
                      prop="name"
                      :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                      :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
              >
                <BaseInput v-model="rate" disabled/>
              </a-form-model-item>
              </a-col>
              <a-col
                      :md="8"
                      :sm="8"
              >
                <a-form-item
                        label="填报人"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 18 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="fromObj.filledBy" disabled/>
                </a-form-item>
              </a-col>
              <a-col
                      :md="8"
                      :sm="8"
              >
                <a-form-item
                        label="填报时间"
                        :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                        :wrapper-col="{ lg: { span: 18 }, sm: { span: 17 } }"
                >
                  <BaseInput v-model="fromObj.createTime" disabled/>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-card>
      </div>
    </template>
    <!-- 列表 -->
    <a-card>
      <titleName title="优化清单" />
      <a-table
        :scroll="{x:true}"
        row-key="id"
        bordered
        :columns="enclosure"
        :pagination="false"
        :data-source="dataSource"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template
                slot="operation"
                slot-scope="text, record"
        >
          <a @click="look(record)">查看</a>
        </template>
      </a-table>
    </a-card>
    <div v-if="!isConsultation" class="tool-btns page-btn-right-top">
      <a-button
        v-if="checkVisibleInDetail"
        class="ml12"
        type="primary"
        @click="handleClose"
      >
        关闭
      </a-button>
    </div>
    <EditFrom
            v-if="visible"
            :visible="true"
            :model="transmitList"
            :project-id="fromObj.projectId"
            :dept-id="deptId"
            :projectInfo="projectInfo"
            :view="true"
            @cancel="handleCancel"
    />
    </a-spin>
  </base-breadcrumb>
</template>

<script>
import { detailed, queryHistoryDetail } from '@/api/technical/optimizationPlan'
import { checkVisibleInDetail} from '@/utils/util'
import EditFrom from './addToReporting/EditFrom'
const enclosure = [
    {
        title: '序号',
        width: '65px',
        scopedSlots: { customRender: 'serial' },
    },
    {
        title: '双优化名称',
        dataIndex: 'name',
    },
    {
        title: '双优化分类',
        dataIndex: 'categoryName',
    },
    {
        title: '内容概述',
        dataIndex: 'content',
      width: 300,
    },
    {
        title: '计划实施时间',
        dataIndex: 'startTime',
      width: 120,
    },
    {
        title: '提出人',
        dataIndex: 'proposer',
    },
    {
        title: '拟实施人',
        dataIndex: 'implement',
    },
    {
        title: '策划效益(万)',
        dataIndex: 'benefit',
      width: 120,
    },
    {
        title: '立项状态',
        dataIndex: 'statusName',
    },
    {
        title: '立项时间',
        dataIndex: 'propTime',
    },
  {
    title: '操作',
    dataIndex: 'operation',
    width: '120px',
    fixed: 'right',
    scopedSlots: { customRender: 'operation' },
  },
]
export default {
    name:'a'+Date.now(),
    components: {
      EditFrom
    },
    data() {
        this.enclosure = enclosure
        return {
          pageLoading: false,
          visible: false,
          projectInfo: {},
             checkVisibleInDetail:checkVisibleInDetail(this),
            fromObj: {
              isSubProject: false
            },
            dataSource: [],
          transmitList: {}
        }
    },
    props: {
      // 是否作为审批详情
      isAudit: {
        type: Boolean,
        default: false
      },
      // 是否作为意见征询
      isConsultation: {
        type: Boolean,
        default: false
      }
    },
    computed: {
      rate() {
        if(this.ownerValue&&this.fromObj.rate) {
          return ((this.fromObj.rate/this.ownerValue) * 100).toFixed(2) + '%'
        }
        return 0
      },
      ownerValue() {
        if(this.fromObj.isSubProject) {
          // 是内部分包项目
          return this.projectInfo.contractAmount
        } else {
          if( this.projectInfo.ownerContractValue) {
            // 不是内部分包项目 contractAmount // 其内部分包项目的“分包合同额”
            return (parseInt((this.projectInfo.ownerContractValue*100).toFixed()) -  parseInt((this.fromObj.subContractAmount*100).toFixed()))/100
          }
        }
        return ''
      }
    },
    mounted() {
      this.pageLoading =true
      if(this.$route.query.version) {
        queryHistoryDetail(this.$route.query.id).then((res) => {
          this.pageLoading =false
          if (res.code === 200) {
            this.fromObj = res.data
            res.data.doubleOptimizationDetailsHistory&&res.data.doubleOptimizationDetailsHistory.forEach(w=>{
              if(w.status) {
                w.statusName = { PAST: '审批通过', REJECTED: '审批不通过', DRAFT: '待立项', SUBMITTED: '审核中' }[
                        w.status
                        ]
              }
            })
            this.dataSource = res.data.doubleOptimizationDetailsHistory
          } else {
            this.$message.error('加载失败')
          }
        })
      } else {
        detailed(this.$route.query.id).then((res) => {
          this.pageLoading =false
          if (res.code === 200) {
            this.fromObj = res.data
            res.data.doubleOptimizationTypeVO&&res.data.doubleOptimizationTypeVO.forEach(w=>{
              if(w.status) {
                w.statusName = { PAST: '立项审批通过', REJECTED: '立项审批不通过', DRAFT: '立项待提交', SUBMITTED: '立项审核中' }[
                        w.status
                        ]
              }else {
                w.statusName = '待立项'
              }
            })
            this.dataSource = res.data.doubleOptimizationTypeVO
          } else {
            this.$message.error('加载失败')
          }
        })
      }

    },
    methods: {
      look(a) {
        if (!Array.isArray(a.implementId)) {
          a.implementId = a.implementId?.split(',')
          a.proposerId = a.proposerId?.split(',')
        }

        console.log(a)
        this.transmitList = a
        this.visible = true
      },
      handleCancel() {
        this.visible = false
      },
      getProjectInfo(info) {
        this.deptId = info.projectDeptOrgId
        if(info.projectFormatName?.length) {
          info.projectFormatName = info.projectFormatName.join(",")
        }
        if(info.innerPackageCode === 0) {
          info.ownerValue = +info.contractValue - (info.ownerContractValue)
        } else {
          info.ownerValue = info.ownerContractValue
        }
        this.projectInfo = info
      },
        handleClose() {
            if (this.isAudit) {
                window.close()
            } else {
                this.$multiTab.close(this.$route.fullpath)
            }
        },
    },
}
</script>

<style>
</style>
